package com.socialuni.model.DO.user;

import lombok.Data;

import javax.persistence.*;
import java.util.Date;

/**
 * 登录相关，只有登录时才用得到的表
 * 自己表示字段，其他表示关联的表内字段
 */
@Data
@Entity
@Table(name = "s_user_account",
        //查询条件索引
        indexes = {
                //关联需要键索引，索引列不能为空
                @Index(columnList = "provider"),
                @Index(columnList = "userId"),
                @Index(columnList = "unionId"),
        },
        uniqueConstraints = {
                //每个渠道都是唯一的
                @UniqueConstraint(columnNames = {"provider", "unionId"}),
                //根据平台和provider和userid获取用户，推送的时候使用？为了确保平台+渠道+user唯一
                @UniqueConstraint(columnNames = {"provider", "userId"}),
                //用来获取某一渠道的token，根据token获取账户
                @UniqueConstraint(columnNames = {"provider", "token"}),
        }
)
public class SocialUserAccountDO {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @Column(nullable = false)
    private Integer userId;
    //使用哪个平台注册的
    @Column(nullable = false)
    private String platform;
    @Column(nullable = false)
    private String provider;
    @Column(nullable = false, columnDefinition = "varchar(20) default ''")
    private String appOpenId;
    @Column(nullable = false, columnDefinition = "varchar(20) default ''")
    private String mpOpenId;
    @Column(nullable = false)
    private String unionId;
    //邀请你的用户
    //微信绑定手机号时使用的，所以存储一下
    @Column(nullable = false)
    private String token;
    @Column(nullable = false)
    private Date createTime;
    @Column(nullable = false)
    private Date updateTime;
    //app推送使用
    private String clientId;

    public SocialUserAccountDO() {
    }

    public SocialUserAccountDO(SocialUserDO user) {
        this.userId = user.getId();
        this.platform = user.getPlatform();
        this.provider = user.getProvider();
        Date curDate = new Date();
        this.createTime = curDate;
        this.updateTime = curDate;
        this.token = "";
    }
}